package cn.edu.lingnan.service.imp;

import cn.edu.lingnan.dao.ScoreMapper;
import cn.edu.lingnan.pojo.Score;
import cn.edu.lingnan.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Scanner;

@Service
public class ScoreServiceImp implements ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Override
    public Integer insert() {
        Score score=new Score();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学号sid");
        score.setSid(sc.next());
        System.out.println("请输入课程号cid");
        score.setCid(sc.next());
        System.out.println("请输入成绩grade");
        score.setGrade(sc.nextInt());
        System.out.println("请输入分数标志");
        score.setScflag(sc.nextInt());
        return scoreMapper.insert(score);
    }

    @Override
    public Integer deleteById() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入需要删除分数的学号");
        String sid=sc.next();
        System.out.println("请输入需要删除分数的课程号");
        String cid=sc.next();
        return scoreMapper.deleteById(sid,cid);
    }

    @Override
    public Integer deleteBySid() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学生id");
        String sid=sc.next();
        return scoreMapper.deleteBySid(sid);
    }

    @Override
    public Integer update() {
        Score score=new Score();
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入需要修改成绩的学号");
        score.setSid(sc.next());
        System.out.println("请输入需要修改成绩的课程号");
        score.setCid(sc.next());
        System.out.println("请输入修改后的分数");
        score.setGrade(sc.nextInt());
        System.out.println("请输入修改后的分数标志");
        score.setScflag(sc.nextInt());
        return scoreMapper.update(score);
    }

    @Override
    public List<Score> getById() {
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入学号进行查询分数");
        String sid=sc.next();
        return scoreMapper.getById(sid);
    }

    public void ScoreAction(){
        System.out.println("                --欢迎进入分数信息管理系统--");
        System.out.println("----------------------------------------------------------------");
        while (true){
            System.out.println(" 1:添加分数信息  2:删除分数信息 3:修改分数信息 4:查询分数信息 0:退出");
            Scanner sc=new Scanner(System.in);
            Integer  selectflag=sc.nextInt();
            if (selectflag ==0){
                System.out.println("已退出分数信息管理系统");
                break;
            }
            switch (selectflag){
                case 1:Integer n1=insert();
                    System.out.println("成功插入"+n1+"条数据");
                    break;

                case 2:
                    while (true){
                        System.out.println("1:根据sid删除所有信息 2:根据sid和cid删除一条信息 3:退出");
                        Integer flag=sc.nextInt();
                        if(flag==1){

                            Integer count=deleteBySid();
                            System.out.println(count);
                        }
                        else if (flag==2){

                            Integer count1=deleteById();
                            System.out.println(count1);
                        }
                        else break;
                    }
                    break;

                case 3:
                    Integer n= update();
                    System.out.println("成功修改"+n+"条数据");
                    break;

                case 4:
                    List< Score> list =getById();
                    System.out.println(list);
                    break;
                default:
                    System.out.println("您的输入有误");
                    break;
            }

        }
    }
}
